home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
edit
/
mg2a_src.zip
/
TERMLIB
/
TGETENT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1988-08-23
|
13KB
|
467 lines
/************************************************************************
* *
* Copyright (c) 1982, Fred Fish *
* All Rights Reserved *
* *
* This software and/or documentation is released for public *
* distribution for personal, non-commercial use only. *
* Limited rights to use, modify, and redistribute are hereby *
* granted for non-commercial purposes, provided that all *
* copyright notices remain intact and all changes are clearly *
* documented. The author makes no warranty of any kind with *
* respect to this product and explicitly disclaims any implied *
* warranties of merchantability or fitness for any particular *
* purpose. *
* *
************************************************************************
*/
/*
* Modified:
* 30-Apr-86 Mic Kaczmarczik ...!ihnp4!seismo!ut-sally!ut-ngp!mic
* Instead of using VAX C getenv("TERM"), which does not
* return the value of logical name "TERM", translate the
* logical name by hand.
* 11-Oct-86 Mic Kaczmarczik ...!ihnp4!seismo!ut-sally!ut-ngp!mic
* Support tc capability to allow the library to use standard
* termcaps. Rewrote tgetent to look for tc capability
* and add new terminal definition to the caller's buffer.
* This makes it rather possible to overflow the caller's
* buffer, but the library doesn't make any claim that it
* won't overwrite the buffer anyway...
* 27-Jan-88 Bob Larson blarson@ecla.usc.edu
* Add primos (__50SERIES) support.
* 1-Feb-88 Sandra Loosemore (sandra@cs.utah.edu)
* Change default termcap file for VMS to be the same place
* that GNU puts it.
* 23-Apr-88 Bob Larson
* merge primos and vms changes
*/
/*
* LIBRARY FUNCTION
*
* tgetent load buffer with entry for specified terminal
*
* KEY WORDS
*
* termcap functions
* utility routines
*
* SYNOPSIS
*
* int tgetent(bp,name)
* char *bp;
* char *name;
*
* DESCRIPTION
*
* Extracts the entry for terminal <name> from the termcap file
* and places it in the character buffer <bp>. It is currently
* assumed that bp is at least 1024 characters. If the entry in
* the termcap file is larger than 1023 characters the excess
* characters will be discarded and appropriate status will
* be returned.
*
* Also note that since bp is used by other termcap
* routines, the storage associated with the termcap entry
* cannot be freed until all termcap calls are completed.
*
* Tgetent can be directed to look in a file other than
* the default (/etc/termcap) by defining an environment
* variable called TERMCAP to be the pathname of the desired
* termcap file. This is useful for debugging new entries.
* NOTE: the pathname MUST begin with a '/' character.
*
* Also, if the string assigned to TERMCAP does not begin with
* a '/' and if the environment variable TERM matches <name> then
* the string assigned to TERMCAP is copied to buffer <bp>
* instead of reading a termcap file.
*
* RETURNS
*
* -1 if the termcap file cannot be opened
* 0 if no entry in termcap file matches <name>
* 1 if extraction is successful with no errors
* 2 if extraction is successful but entry truncated
*
* SEE ALSO
*
* tgetnum extract numeric type capability
* tgetflag test boolean type capability
* tgetstr get string value of capability
*
* AUTHOR
*
* Fred Fish
*
*/
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define BUFSIZE 1024 /* Assumed size of external buffer */
#define NO_FILE -1 /* Returned if can't open file */
#define NO_ENTRY 0 /* Returned if can't find entry */
#define SUCCESS 1 /* Returned if entry found ok */
#define TRUNCATED 2 /* Returned if entry found but trunc */
#ifdef __50SERIES
#define DEFAULT_FILE "mg*>termcap"
#else
#define DEFAULT_FILE "/etc/termcap" /* default termcap filename */
#ifdef VAXC
#define index strchr
#endif
#endif
char *_tcpbuf; /* Place to remember buffer pointer */
FILE *fp; /* Termcap file */
static FILE *find_file();
extern char *index();
/*
* PSEUDO CODE
*
* Begin tgetent
* Erase any previous buffer contents.
* Remember the buffer pointer.
* If termcap file is not found then
* If buffer was filled anyway then
* Return SUCCESS.
* Else
* Return NO_FILE.
* End if
* Else
* Find entry associated with name
* While an entry was found and limit not reached
* If no tc capability found Then
* Exit while loop with status = SUCCESS
* Else
* Call getent to get entry indicated by tc=
* If entry not found then
* Exit loop with status != SUCCESS
* End if
* Concatenate entry into buffer
* End If
* End while
* End if
* Close termcap file
* Return status code
* End tgetent
*
*/
static int getent();
int tgetent(bp,name)
char *bp;
char *name;
{
char *tc, *tcbufp, tcbuf[80], termbuf[BUFSIZE], *tgetstr();
char *bufp, *cp; /* current start of buffer */
int limit = 10; /* maximum nesting */
int status; /* return from getent() */
*bp = '\0'; /* clear buffer */
_tcpbuf = bp; /* save base of buffer */
/* Look for termcap file. If NULL, find_file may have found a */
/* a valid termcap string in the environment variable TERMCAP. */
/* If non-null, attempt to find the entry in the termcap file */
if ((fp = find_file(bp)) == NULL) {
if (*bp != NULL)
return(SUCCESS);
else
return(NO_FILE);
}
status = getent(bp, name);/* look for main entry */
/* Start looking for tc capabilities in the termcap. If
* found, concatenate the entry for the new terminal to the
* current buffer and try again. To avoid infinite loops,
* allow only 10 loops through this process.
*/
while ((status == SUCCESS) && limit--) {
/* look for tc capability. If none found, exit loop */
tcbufp = tcbuf;
if (((tc = tgetstr("tc",&tcbufp)) == NULL)
|| (*tc == '\0')) {
status = SUCCESS;/* no more tc= entries */
break;
}
/* Attempt to get next entry. Exit loop if unsuccessful */
if ((status = getent(termbuf, tcbuf)) != SUCCESS)
break;
/* Copy new entry into buffer, right at "tc=" */
for (bufp = bp; *bufp; bufp++) /* find tc= */
if ((*bufp=='t') && (bufp[1]=='c') && (bufp[2]=='='))
break;
if ((cp = index(termbuf,':')) == NULL)
cp = termbuf;
strcpy(bufp, cp + 1);
}
/* close termcap file and return the status */
fclose(fp);
return status;
}
/*
* INTERNAL FUNCTION
*
* getent find termcap entry in termcap file
*
* KEY WORDS
*
* internal functions
* getent
*
* SYNOPSIS
*
* static int getent(bp,name)
* char *bp;
* char *name;
*
* DESCRIPTION
*
* Getent is called by tgetent each time tgetent attempts to
* read a capability from the termcap database file. Places
* the entry in the buffer pointed to by bp
*
*
* PSEUDOCODE
*
* Begin Getent
* Seek to beginning of termcap file
* Clear buffer
* While records left to process
* If this is entry is what we want then
* If entry was truncated then
* Return TRUNCATED status
* Else
* Return SUCCESS status.
* End if
* End if
* End while
* Return NO_ENTRY status.
* End
*/
static int getent(bp,name)
char *bp; /* Pointer to buffer (1024 char min) */
char *name; /*